//import * as config from '../utils/config';
import wepy from 'wepy';

import * as OauthApi from './OauthApi';

/**
 * @description 获取用户资料
 * @param {Boolean} reload 是否重新远程获取[true/false]
 */
export async function getUserInfo(reload) {
    let config = wepy.$instance.globalData.globalConfig;
    try {
        let times = 0;
        if (arguments.length > 1) {
            times = arguments[1];
        }
        const activeUser = await OauthApi.getActiveUserFromStorage();
        if (!activeUser) {
            return {
                error: 'not_login'
            };
        }

        let users = await OauthApi.getUserInfoFromStorage();
        if (reload) {
            const userInfo = await OauthApi.getWithToken(config.BASE_URL + "/api/v2/member/list"); // 获取用户信息
            if (userInfo.error && userInfo.error !== 'success') {
                if (times < 4) {
                    if (userInfo.error === 'server_error')
                        return getUserInfo(reload, ++times);
                    else if (userInfo.error === 'invalid_token') {
                        await OauthApi.resetAccessToken();
                        return getUserInfo(reload, ++times);
                    } else if (userInfo.error === 'not_login') {
                        return {
                            error: userInfo.error
                        };
                    }
                }
                return {
                    error: userInfo.error
                };
            }
            // users[userInfo.data.openid] = Object.assign(users[userInfo.data.openid], userInfo.data);
            users["openid"] = Object.assign(users["openid"], userInfo.data);
            await OauthApi.setUserInfoToStorage(users);
        }
        let token = users[activeUser];
        return token;
    } catch (error) {
        console.error('error:' + error);
        return error;
    }
}

/**
 * @description 修改用户信息
 * @param {String} face 微信头像
 * @param {String} username 微信昵称
 */
export async function updateMember(face, username) {
    let config = wepy.$instance.globalData.globalConfig;
    try {
        let times = 0;
        if (arguments.length > 2) {
            times = arguments[2];
        }
        let result = await OauthApi.postWithToken(config.BASE_URL + "/api/v2/member/update", {
            face: face,
            username: username
        });
        if (result.error && result.error !== 'success') {
            if (times < 4) {
                if (result.error === 'server_error')
                    return updateMember(face, username, ++times);
                else if (result.error === 'invalid_token') {
                    await OauthApi.resetAccessToken();
                    return updateMember(face, username, ++times);
                } else if (result.error === 'not_login') {
                    return {
                        error: 'not_login'
                    };
                }
            }
            return {
                error: result.error
            };
        }

        return result;
    } catch (error) {
        console.error('error:' + error);
        return error;
    }
}